package log350.tp3;

import java.util.ArrayList;
import java.util.Collections;

import classes.ForumHandler;
import classes.Forums;
import classes.User;
import classes.UserHandler;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ForumMainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forum_main);
		
		TableLayout tableLayoutLink = (TableLayout)findViewById(R.id.tblLinkForum);
        tableLayoutLink.removeAllViews();
        
        TableRow tableRowLink = new TableRow(getApplicationContext());            
        
        final TextView lanETS = new TextView(getApplicationContext());
        lanETS.setText("Lan ETS");  
        lanETS.setTextColor(0xFFff8000);
        lanETS.setTextSize(12);
        lanETS.setPaintFlags( lanETS.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
       
        lanETS.setPadding(15, 10, 0, 10);
        lanETS.setClickable(true);
        lanETS.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				setResult(RESULT_OK, new Intent());
		        finish();
			}

          });
        tableRowLink.addView(lanETS);
        
        final TextView firstComma = new TextView(getApplicationContext());
        firstComma.setText(">");
        firstComma.setTextSize(12);
        firstComma.setPadding(10, 10, 0, 10);
        tableRowLink.addView(firstComma);
        
        final TextView forum = new TextView(getApplicationContext());
        forum.setText("Forum");   
        forum.setTextSize(12);        
        forum.setPadding(10, 10, 0, 10);
        tableRowLink.addView(forum);
        
        tableLayoutLink.addView(tableRowLink);
        
		TableLayout tableLayout = (TableLayout)findViewById(R.id.tabForum);
        tableLayout.removeAllViews();
        
        for(int i=0;i< ForumHandler.getInstance().getAllForums().size();i++){
            final Forums row  = ForumHandler.getInstance().getAllForums().get(i);
            TableRow tableRow = new TableRow(getApplicationContext());  
            
            final ImageView columsViewLogo = new ImageView(getApplicationContext());
            columsViewLogo.setImageResource(R.drawable.new_messages);
            columsViewLogo.setPadding(20, 25, 20, 25);    
            tableRow.addView(columsViewLogo);
            
            
            final LinearLayout linearLayout = new LinearLayout(getApplicationContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            
            final TextView columsViewName = new TextView(getApplicationContext());
            columsViewName.setText(row.getName());   
            columsViewName.setTextColor(0xFFff8000);
            columsViewName.setTextSize(17);
            columsViewName.setPaintFlags( columsViewName.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
           
            columsViewName.setPadding(15, 10, 0, 10);
            columsViewName.setClickable(true);
            columsViewName.setOnClickListener(new OnClickListener(){
    			public void onClick(View arg0) {
    				IntentHelper.addObjectForKey(row, "ForumToSujet");
    				Intent intent = new Intent(getApplicationContext(), ForumSujetActivity.class);
    		    	startActivityForResult(intent,0);
    			}

              });

            linearLayout.addView(columsViewName);

            final TextView columsViewDescription = new TextView(getApplicationContext());
            columsViewDescription.setText(row.getDescription());
            columsViewDescription.setPadding(10, 10, 10, 30);
            columsViewDescription.setTextSize(13);
            linearLayout.addView(columsViewDescription);
            tableRow.addView(linearLayout);
            
            tableLayout.addView(tableRow);
            
        }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_forum_main, menu);
		return true;
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == Activity.RESULT_OK && requestCode == 0) {
    		setResult(RESULT_OK, new Intent());
	        finish();
    	}
    }
}
